//
//  MBTheComCell.m
//  MiBaoTu
//
//  Created by 彭鹏 on 16/9/18.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBTheComCell.h"

@implementation MBTheComCell
+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *identify = @"MBTheComCell";
    MBTheComCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"MBTheComCell" owner:self options:nil] lastObject];
    }
    return cell;
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setDataDic:(NSDictionary *)dataDic
{
    _dataDic = dataDic;
    [self setNeedsLayout];
}

- (int)onSolveTimeWithStart:(NSString *)start withEndTimer:(NSString *)end
{
    NSDate *dateStart = [NSDate dateWithTimeIntervalSince1970:[start doubleValue]];
    NSDate *dateEnd = [NSDate dateWithTimeIntervalSince1970:[end doubleValue]];
    NSDate *date = [NSDate date];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyyMMdd"];
    NSString *startStr = [dateFormatter stringFromDate:dateStart];
    NSString *endStr = [dateFormatter stringFromDate:dateEnd];
    NSString *nowStr = [dateFormatter stringFromDate:date];
    
    if ([nowStr intValue] >= [startStr intValue] && [nowStr intValue] <= [endStr intValue]) {
        //进行
        return 1;
    }
    else if ([nowStr intValue] > [endStr intValue]) {
        //结束
        return 2;
    }
    return 0;
}

- (NSString *)onGetTimeWithStart:(NSString *)start withEndTimer:(NSString *)end
{
    NSDate *dateStart = [NSDate dateWithTimeIntervalSince1970:[start doubleValue]];
    NSDate *dateEnd = [NSDate dateWithTimeIntervalSince1970:[end doubleValue]];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"MM.dd"];
    NSString *startStr = [dateFormatter stringFromDate:dateStart];
    NSString *endStr = [dateFormatter stringFromDate:dateEnd];
    
    return [NSString stringWithFormat:@"%@-%@",startStr,endStr];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    if (!_dataDic) return;
    
    //活动名称
    _titleLb.text = [_dataDic objectForKey:@"ActivityName"];
    
    //图片
    NSString *picPath = [_dataDic objectForKey:@"PicPath"];
    if ([picPath isKindOfClass:[NSNull class]]) {
        picPath = @"";
    }
    if (picPath.length == 0) {
        _bigImageView.image = [UIImage imageNamed:@"huodong_moren_bg"];
    } else {
        [_bigImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",MUSIC_PLAY_Url,picPath]] placeholderImage:[UIImage imageNamed:@"huodong_moren_bg"]];
    }
    
    //时间
    NSString *StartTime = [_dataDic objectForKey:@"StartTime"];
    NSString *EndTime = [_dataDic objectForKey:@"EndTime"];
    int isActivity = [self onSolveTimeWithStart:StartTime withEndTimer:EndTime];
    if (isActivity == 1) {
        _rightImageView.image = [UIImage imageNamed:@"jinxingzhong"];
    }
    else if (isActivity == 2) {
        _rightImageView.image = [UIImage imageNamed:@"yijieshu"];
    }
    else {
//        _rightImageView.image = [UIImage imageNamed:@"yijieshu"]; 
    }
    _activityLb.text = [NSString stringWithFormat:@"活动时间: %@",[self onGetTimeWithStart:StartTime withEndTimer:EndTime]];
    
    //多少人
    NSString *Nums = [_dataDic objectForKey:@"Nums"];
    _numLb.text = [NSString stringWithFormat:@"参与人数: %@人",Nums];
}

@end
